package com.bpack;

import com.bpack.LoginActivity.RequestMethod;
import com.rest.BpackConnect;
import com.rest.JsonParse;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * ItemInfo - Item Screen
 * User will see this screen when an item is clicked from the ToDo screen
 * Depending on the item, user can view item details and approve or deny requests
 * @author Daranhdara Hun
 */
public class ItemInfo extends Activity{
	/*
	 *  Item variables and values
	 *  item[0] - Array of "labels" key names
	 *  item[1] - Array of "labels" key name values
	 *  item[2] - Array of "data" key names
	 *  item[3] - Array of "data" key names values
	 */
	String[][] item = new String[4][1];

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listviewmain);

		// Get intent from last activity to obtain id and key for uri construction
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		String id = extras.getString("id");
		String key = extras.getString("key");
		String uri = "https://p2p.b-pack.com/a/endemo/api3/data/" + key + "/" + id + "?oauth_token=621d0e5a6039e393dd045cfd437aa276";

		// Instantiate LinearLayout and XML layout
		LinearLayout l = (LinearLayout) findViewById(R.id.mylayout1);
		LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		// Instantiate JsonParse
		JsonParse parser = new JsonParse();

		// Populate the item 2d array with the data corresponding to item user clicked
		BpackConnect client = new BpackConnect(uri);
		try{
			client.Execute(RequestMethod.GET);
			item = parser.parseToDoItems(client.getResponse());
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this, "Your Item has no information", 0);
		}
		
		// Display the information. Currently only shows the first 5 items.
		for (int i=0; i<5; i++){
			View customView = linflater.inflate(R.layout.todosubcategory,null);
			TextView tv = (TextView) customView.findViewById(R.id.editText1);
			tv.setId(i);
			tv.setText(item[1][i] + ": " + item[3][i]);
			Button bv = (Button) customView.findViewById(R.id.button1);
			// Because each element within a layout will show since we're calling the layout, 
			// We need to suppress the other elements we don't want to be seen, hence"bv.setVisibility(View.GONE);"
			bv.setVisibility(View.GONE);
			l.addView(customView);
			
		}
		
		
		
		
		
		
		
		// THESE BUTTONS WILL NEED TO BE DYNAMIC BASED ON JSON
		addButton("Approve", l, linflater);
		addButton("Reject", l, linflater);
	}

	/**
	 * addButton 
	 * Adds a button to the provided layout
	 * @param label - label that goes on the button
	 * @param l - LinearLayout instance
	 * @param linflater - LayoutInflater instance
	 */
	private void addButton(String label, LinearLayout l, LayoutInflater linflater){
		View customView = linflater.inflate(R.layout.todosubcategory,null);
		TextView tv = (TextView) customView.findViewById(R.id.editText1);
		tv.setVisibility(View.GONE);
		Button bv = (Button) customView.findViewById(R.id.button1);
		bv.setId(1);
		bv.setText(label);
		l.addView(customView);
	}
}
